<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- :before-upload传了一个函数过去 -->
      <upload-excel :before-upload="beforeUpload" :on-success="successFn" />
    </div>
  </div>
</template>

<script>
import EmployeeEnum from '@/contant/employee'
import { importEmployee } from '@/api/employees'
import { parseTime } from '@/utils'
const { mapKeyPath } = EmployeeEnum
export default {
  filters: {},
  components: {},
  data() {
    return {}
  },
  computed: {},
  watch: {},
  created() {},
  methods: {
    beforeUpload(file) {
      if (file.name.endsWith('.xlsx')) {
        return true
      }
      return alert('请选择excel文件')
    },
    async successFn({ header, results }) {
      // console.log(header, results)
      // 这样的数据写死了
      // const data = results.map((item) => {
      //   return {
      //     correctionTime: item.转正日期,
      //     mobile: item.手机号,
      //     timeOfEntry: item.入职日期,
      //     username: item.姓名,
      //     workNumber: item.工号
      //   }
      // })
      // 数组里的对象数据不是想要的时候，需要重新处理
      const data = results.map(item => {
        const obj = {}
        for (const key in item) {
          if (key === '入职日期' || key === '转正日期') {
            const date = new Date((item[key] - 1) * 24 * 60 * 60 * 1000)
            // console.log(date.setFullYear(date.getFullYear() - 70))
            const time = parseTime(date.setFullYear(date.getFullYear() - 70), '{y}-{m}-{d}')
            obj[mapKeyPath[key]] = time
          } else {
            obj[mapKeyPath[key]] = item[key]
          }
        }
        return obj
      })
      // console.log(data)
      await importEmployee(data)
      this.$message.success('导入成功')
      this.$router.go(-1)
    }
  }
}
</script>

<style scoped>

</style>
